/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_BTREE_PAGES_DISPATCHERS_DISPATCHER_HPP
#define	_CUBEFS_CORE_BTREE_PAGES_DISPATCHERS_DISPATCHER_HPP

#define CUBEFS_SOURCE "cubefs/core/btree/pages/dispatchers/node_dispatcher.hpp:" CUBEFS_AT

namespace cubefs    {
namespace btree     {


using namespace cubefs::tools;
using cubefs::TypeList;

template <typename NodeBase, typename NodeList, typename List2 = NodeList> class NodeDispatcherTool;

template <typename NodeBase, typename List2>
class NodeDispatcherTool<NodeBase, NullType, List2> {
public:
    template <typename Functor>
    static void Dispatch(NodeBase *, Functor &) {
        throw DispatchException(CUBEFS_SOURCE, "Can't dispatch btree node type");
    }

    template <typename Functor>
    static void Dispatch(NodeBase *node1, NodeBase *node2, Functor &functor) {}

    template <typename Functor>
    static void DoubleDispatch(NodeBase *node1, NodeBase *node2, Functor &functor) {}

    template <typename Functor>
    static void Dispatch2(NodeBase *node1, NodeBase *node2, Functor &functor) {}

    static void BuildMetadataList(MetadataList &list) {}
};


template <typename NodeBase, typename List>
class NodeDispatcherTool<NodeBase, List, NullType> {
public:
    template <typename Functor>
    static void Dispatch(NodeBase *, Functor &) {
        throw DispatchException(CUBEFS_SOURCE, "Can't dispatch btree node type");
    }

    template <typename Functor>
    static void Dispatch(NodeBase *node1, NodeBase *node2, Functor &functor) {
        throw DispatchException(CUBEFS_SOURCE, "Can't dispatch btree node type");
    }

    template <typename Functor>
    static void DoubleDispatch(NodeBase *node1, NodeBase *node2, Functor &functor) {
        throw DispatchException(CUBEFS_SOURCE, "Can't dispatch btree node type");
    }

    template <typename Functor>
    static void Dispatch2(NodeBase *node1, NodeBase *node2, Functor &functor) {
        throw DispatchException(CUBEFS_SOURCE, "Can't dispatch btree node type");
    }

    static void BuildMetadataList(MetadataList &list) {}
};

template <typename NodeBase>
class NodeDispatcherTool<NodeBase, NullType, NullType> {
public:
    template <typename Functor>
    static void Dispatch(NodeBase *, Functor &) {
        throw DispatchException(CUBEFS_SOURCE, "Can't dispatch btree node type");
    }

    template <typename Functor>
    static void Dispatch(NodeBase *node1, NodeBase *node2, Functor &functor) {
        throw DispatchException(CUBEFS_SOURCE, "Can't dispatch btree node type");
    }

    template <typename Functor>
    static void DoubleDispatch(NodeBase *node1, NodeBase *node2, Functor &functor) {
        throw DispatchException(CUBEFS_SOURCE, "Can't dispatch btree node type");
    }

    template <typename Functor>
    static void Dispatch2(NodeBase *node1, NodeBase *node2, Functor &functor) {
        throw DispatchException(CUBEFS_SOURCE, "Can't dispatch btree node type");
    }

    static void BuildMetadataList(MetadataList &list) {}
};


template <typename NodeBase, typename Head1, typename Tail1, typename Head2, typename Tail2>
class NodeDispatcherTool<NodeBase, TypeList<Head1, Tail1>, TypeList<Head2, Tail2> > {
    static const bool   Root1  = Head1::Descriptor::Root;
    static const bool   Leaf1  = Head1::Descriptor::Leaf;
    static const BigInt Level1 = Head1::Descriptor::Level;

    static const bool   Root2  = Head2::Descriptor::Root;
    static const bool   Leaf2  = Head2::Descriptor::Leaf;
    static const BigInt Level2 = Head2::Descriptor::Level;

public:
    template <typename Functor>
    static void Dispatch(NodeBase *node, Functor &functor) {
        bool level1EQ = Level1 == ANY_LEVEL ? true : Level1 == node->level();

        if (level1EQ && Root1 == node->is_root() && Leaf1 == node->is_leaf()) {
            functor(static_cast<Head1*>(node));
        }
        else {
            NodeDispatcherTool<NodeBase, Tail1>::Dispatch(node, functor);
        }
    }

    template <typename Functor>
    static void Dispatch(NodeBase *node1, NodeBase *node2, Functor &functor) {
        bool level1EQ = Level1 == ANY_LEVEL ? true : Level1 == node1->level();

        if (level1EQ && Root1 == node1->is_root() && Leaf1 == node1->is_leaf()) {
            functor(static_cast<Head1*>(node1), static_cast<Head1*>(node2));
        }
        else {
            NodeDispatcherTool<NodeBase, Tail1>::Dispatch(node1, node2, functor);
        }
    }

    template <typename Functor>
    static void DoubleDispatch(NodeBase *node1, NodeBase *node2, Functor &functor) {
        bool level1EQ = Level1 == ANY_LEVEL ? true : Level1 == node1->level();
        if (level1EQ && Root1 == node1->is_root() && Leaf1 == node1->is_leaf()) {
            Dispatch2(node1, node2, functor);
        }
        else {
            NodeDispatcherTool<NodeBase, Tail1, TypeList<Head2, Tail2> >::DoubleDispatch(node1, node2, functor);
        }
    }


    template <typename Functor>
    static void Dispatch2(NodeBase *node1, NodeBase *node2, Functor &functor) {
        bool level2EQ = Level2 == ANY_LEVEL ? true : Level2 == node2->level();

        if (level2EQ && Root2 == node2->is_root() && Leaf2 == node2->is_leaf()) {
            functor(
                static_cast<Head1*>(node1),
                static_cast<Head2*>(node2)
            );
        }
        else {
            NodeDispatcherTool<NodeBase, TypeList<Head1, Tail1>, Tail2>::Dispatch2(node1, node2, functor);
        }
    }

    static void BuildMetadataList(MetadataList &list) {
        Head1::Init();
        list.push_back(Head1::reflection());
        NodeDispatcherTool<NodeBase, Tail1, Tail1>::BuildMetadataList(list);
    }
};


}
}

#undef CUBEFS_SOURCE
#endif //_CUBEFS_CORE_BTREE_PAGES_DISPATCHERS_DISPATCHER_HPP